package com.twt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Desciption:
 * @ClassName:RedisConfig
 * @Author:TwT
 * @Date:2022/7/24 14:19
 * @Version:1.0
 **/
@Configuration
public class RedisConfig {
    @Bean
    RedisTemplate redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);

        //设置键值默认序列化方式
        RedisSerializer stringSerializer = new StringRedisSerializer(); //RedisTemplate有自己的默认序列化的方式，不过使用默认方式，会在redis客户端查看的时候出现乱码，不便与使用，我们这里用falstjson库
        redisTemplate.setDefaultSerializer(stringSerializer);

        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        return redisTemplate;
    }
}
